package im.starchat.chat.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.rest.model.GiftBean;
import im.starchat.chat.utils.Constants;

/**
 * Created by liuchao on 2/3/16.
 * 预告 fragment
 */
public class GiftFragment extends Fragment {

    private static final String TAG = "EmotionFragment";

    public static final String GIFT_LIST = "gift_list";

    @InjectView(R.id.gv_emotion)
    GridView mEmotion;

    private GiftShowAdapter mGiftAdapter;

    private ArrayList<GiftBean> mGiftList ;

    public GiftFragment() {
    }

    public static GiftFragment newInstance(ArrayList<GiftBean> giftBeans) {
        GiftFragment fragment = new GiftFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(GIFT_LIST, giftBeans);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mGiftList = getArguments().getParcelableArrayList(GIFT_LIST);
        View view = inflater.inflate(R.layout.fragment_emotion, container, false);
        ButterKnife.inject(this, view);

        mGiftAdapter = new GiftShowAdapter(getActivity());
        mEmotion.setAdapter(mGiftAdapter);

        mEmotion.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // 每个条目的点击监听事件
                if (Constants.sChooseId != Constants.NULL_POSITION) {
                    if (Constants.sChooseId == mGiftList.get(position).getId()) {
                        Constants.sChooseId = Constants.NULL_POSITION;
                    } else {
                        Constants.sChooseId = mGiftList.get(position).getId();
                    }
                } else {
                    Constants.sChooseId = mGiftList.get(position).getId();
                }
                mGiftAdapter.notifyDataSetChanged();
            }

        });

        return view;
    }

    public void refreshGiftView(){
        if(mGiftAdapter != null){
            mGiftAdapter.notifyDataSetChanged();
        }
    }

    class GiftShowAdapter extends BaseAdapter {
        private Context context;

        GiftShowAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mGiftList.size();//imgs.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mGiftList.size();//imgs[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(context, R.layout.item_gift, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            if (!TextUtils.isEmpty(mGiftList.get(position).getImage())) {
                Picasso.with(getActivity()).load(mGiftList.get(position).getImage())
                        .into(imageView);
            }
            TextView diamondCount = (TextView) view.findViewById(R.id.diamond_count);
            diamondCount.setText(mGiftList.get(position).getAmount() + "");
            ImageView chooseCheckbox = (ImageView) view.findViewById(R.id.choose);
            if(Constants.sChooseId == mGiftList.get(position).getId()){
                chooseCheckbox.setVisibility(View.VISIBLE);
            }else {
                chooseCheckbox.setVisibility(View.INVISIBLE);
            }

//            imageView.setBackgroundResource(mGiftList.get(position).getImage());//imgs[position]);
            return view;
        }
    }

}
